package com.sxhuayuan.parking.controller.agent;

import com.alibaba.fastjson.JSON;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.controller.BaseController;
import com.sxhuayuan.parking.entity.Agent;
import com.sxhuayuan.parking.service.*;
import com.sxhuayuan.parking.utils.DateUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 代理统计
 */
@RestController("agentStatisticsController")
@RequestMapping("/agent/statis")
public class StatisticsController extends BaseController {

	@Resource(name = "orderServiceImpl")
	OrderService orderService;

	@Resource(name = "agentServiceImpl")
	AgentService agentService;

	@Resource(name = "parkingLotServiceImpl")
	ParkingLotService parkingLotService;

	@Resource(name = "redisServiceImpl")
	RedisService redisService;

	/**
	 * 统计代理数量和车场数量
	 * 
	 * @return
	 */
	@GetMapping("/agentandparkinglot")
	public RespMessage countAgentAndParkingLot() {
		Long agent = agentService.countAll(agentService.getCurrent());
		Long parkingLot = parkingLotService.countAll(agentService.getCurrent());
		List<Long> list = new ArrayList<>();
		list.add(agent);
		list.add(parkingLot);
		return RespMessage.builder().success().setDatas(list).build();
	}

	/**
	 * 订单统计
	 *
	 * @param type
	 *            时间格式化类型
	 * @param startTime
	 *            起始时间
	 * @param endTime
	 *            终止时间
	 * @param id
	 *            车场id
	 * @return 统计结果 0:时间 1:订单数量 3:订单金额
	 */
	@PostMapping("/echars/countorder")
	public RespMessage cylindricalGraph(@RequestParam String type, @RequestParam String startTime, @RequestParam String endTime, Long id) {
		String sn = null;
		if (id != null) {
			sn = parkingLotService.findOne(id).getSn();
		}
		String formatType = null;
		switch (type) {
		case "date":
			formatType = "%m-%d";
			break;
		case "month":
			formatType = "%Y-%m";
			break;
		default:
			RespMessage.error("查询类型错误");
		}
		List<Map<String, Object>> map = orderService.findCountByDateTime(formatType, startTime, endTime, sn, agentService.getCurrent());
		return successResp(map);
	}

	/** 根据时间查询车场订单饼图数据 */
	@PostMapping("/echars/parkinglot")
	public RespMessage parkingLotOrder(@RequestParam String startTime, @RequestParam String endTime) {
		List<Map<String, Object>> map = orderService.findParkingLotByDateTime(startTime, endTime, agentService.getCurrent());
		return successResp(map);
	}

	/** 查询近30天订单统计数据 折线图 */
	@GetMapping("/echars/brokenlinediagram")
	public RespMessage brokenLineDiagram() {
		Agent current = agentService.getCurrent();
		String redis_order_statis = "agent_order_month_";
		String string = redisService.getString(redis_order_statis + current.getId());
		if (string != null) {
			return successResp(JSON.parseArray(string));
		}

		// 30天以前的00:00:00
		String startTime = DateUtils.format(DateUtils.getStartTimeOfOneday(-30));
		// 前一天的23:59:59
		String endTime = DateUtils.format(DateUtils.getEndTimeOfOneday(-1));
		List<Map<String, Object>> map = orderService.findCountByDateTime("%Y-%m-%d", startTime, endTime, null, current);
		String s = JSON.toJSONString(map);
		redisService.saveString(redis_order_statis + current.getId(), s, DateUtils.nowToTomorrowSecond());
		return successResp(map);
	}

}
